﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using ShapeDemo.UC.WinProp;
using System.Windows;
using System.Windows.Input;
using System.Windows.Shapes;
using System.Windows.Media;

namespace ShapeDemo
{
    public class RectDrawer
    {
        public Canvas RectCanvas { get; set; }
        public List<ASPNETX_WPFRect> RectCollection { get; set; }

        private WinPropRectangle winRectProp;
        private WinPropCommon winCommonProp;

        ASPNETX_WPFRect rectBody;
        private TMouse _MouseType;
        public TMouse MouseTye
        {
            get
            {
                return _MouseType;
            }
            set
            {
                switch (value)
                {
                    case TMouse.Drawing:

                        RectCanvas.Cursor = Cursors.Cross;

                        foreach (ASPNETX_WPFRect item in RectCollection)
                        {
                            item.RectBody.Cursor = Cursors.Cross;
                        }

                        _MouseType = value;

                        break;
                    case TMouse.Point:

                        RectCanvas.Cursor = Cursors.Arrow;

                        foreach (ASPNETX_WPFRect item in RectCollection)
                        {
                            item.RectBody.Cursor = Cursors.Hand;
                            item.RectBody.MouseLeftButtonDown += new MouseButtonEventHandler(RectBody_MouseLeftButtonDown);
                        }

                        _MouseType = value;

                        break;
                }
            }
        }

        bool IsRectInDrawing = false;

        public RectDrawer(Canvas canvasContent, WinPropRectangle winShapeProp, WinPropCommon winProp)
        {
            RectCollection = new List<ASPNETX_WPFRect>();
            
            RectCanvas = canvasContent;
            winRectProp = winShapeProp;
            winCommonProp = winProp;

            _MouseType = TMouse.Drawing;

            RectCanvas.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(rectCanvas_MouseLeftButtonDown);
            RectCanvas.MouseMove += new System.Windows.Input.MouseEventHandler(rectCanvas_MouseMove);

            BindListBox();
        }

        public void Draw()
        { 
            
        }


        #region 画矩形
        private void rectCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (_MouseType == TMouse.Drawing)
            {
                if (IsRectInDrawing == false)
                {
                    rectBody = ResourceCenter.NewRect();
                    RectCanvas.Children.Add(rectBody.RectBody);

                    rectBody.P1 = e.GetPosition(RectCanvas);
                    IsRectInDrawing = true;

                    return;
                }
                if (IsRectInDrawing == true)
                {
                    rectBody.P2 = e.GetPosition(RectCanvas);
                    IsRectInDrawing = false;

                    //添加基本属性
                    rectBody.CommonProperty.Index = winCommonProp.tbIndex.Text;
                    rectBody.CommonProperty.Content = winCommonProp.tbContent.Text;

                    RectCollection.Add(rectBody);

                    BindListBox();

                    return;
                }
            }
        }

        private void rectCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (_MouseType == TMouse.Drawing)
            {
                if (IsRectInDrawing == true)
                {
                    Point p = e.GetPosition(RectCanvas);
                    rectBody.P2 = p;

                    rectBody.RectBody.SetValue(Canvas.LeftProperty, rectBody.Left);
                    rectBody.RectBody.SetValue(Canvas.TopProperty, rectBody.Top);
                    rectBody.RectBody.Width = rectBody.Width;
                    rectBody.RectBody.Height = rectBody.Height;

                    winRectProp.X = rectBody.Left;
                    winRectProp.Y = rectBody.Top;

                    winRectProp.Width = rectBody.Width;
                    winRectProp.Height = rectBody.Height;
                }
            }
        }
        #endregion

        /// <summary>
        /// 绑定各层所添加的图形到列表
        /// </summary>
        public void BindListBox()
        {
            winCommonProp.lstHistory.Items.Clear();
            foreach (ASPNETX_WPFRect item in RectCollection)
            {
                winCommonProp.lstHistory.Items.Add(item.CommonProperty.Content);
            }
        }

        void RectBody_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            foreach (ASPNETX_WPFRect item in RectCollection)
            {
                item.RectBody.Fill = new SolidColorBrush(Colors.Blue);
            }

            Rectangle mrect = (Rectangle)sender;

            foreach (ASPNETX_WPFRect item in RectCollection)
            {
                if (item.RectBody == mrect)
                {
                    winRectProp.SetValue(item);
                    item.RectBody.Fill = new SolidColorBrush(Colors.Red);

                    winCommonProp.tbIndex.Text = item.CommonProperty.Index;
                    winCommonProp.tbContent.Text = item.CommonProperty.Content;
                }
            }
        }
    }
}
